// packageI/taskPackage/task/task.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoArr: [],
    shareVideoArr: [],
    article: [],
    can_recovery: false, //是否已经复活，true是可以重新做任务，false是需要先去支付复活费用
    showShare: false,
    options: [{
        name: "视频",
        icon: `${app.globalData.imgPath}images%2FtaskPackage%2Fshipin.png`,
        router: 'VideoList'
      },
      {
        name: "文章",
        icon: `${app.globalData.imgPath}images%2FtaskPackage%2Fwenzhang.png`,
        router: 'notice'
      }
    ],
    acticeMonth: '', //当前选择的年月
    acticeDay: '',
    page: 1,
    info: {},
    time: '',
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#FF8924',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    });
    wx.setNavigationBarTitle({
      title: '任务包',
    })
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    this.setData({
      acticeMonth: new Date().getFullYear() + '-' + (new Date().getMonth() + 1), //默认当前年月
    })
    this.getData();
    this.getDailyData();
  },
  toMy() {
    wx.navigateTo({
      url: '/packageI/taskPackage/taskPackageMy/taskPackageMy',
    })
  },
  shareHandle(e) {
    let item = e.currentTarget.dataset.item;
    if (item.router == "VideoList") {
      wx.navigateTo({
        url: `/packageC/video_goods/VideoList/VideoList?taskID=${this.data.id}&taskDay=${this.data.acticeDay}`,
      })
    } else {
      wx.navigateTo({
        url: `/packageA/member/notice/notice?taskID=${this.data.id}&taskDay=${this.data.acticeDay}`,
      })
    }
  },
  childGet(e) {
    this.getDailyData(e.detail.date);
  },
  showShareCanle() {
    this.setData({
      showShare: false
    })
  },
  makeTask() {
    this.setData({
      showShare: true
    })
  },
  payTask() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.task-package.frontend.recovery.create"),
      data: {
        record_id: this.data.id,
        day: this.data.acticeDay
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.payTaskCreat(res.data.recovery_order_id);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  payTaskCreat(id) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.task-package.frontend.create.index"),
      data: {
        recovery_order_id: id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          wx.navigateTo({
            url: `/packageD/buy/orderPay/orderPay?order_id=${res.data.order_ids}`,
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.task-package.frontend.record.info"),
      data: {
        id: this.data.id,
        month: this.data.acticeMonth
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info: res.data,
          })
          this.timeCountDown(this.data.info.active_end);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  timeCountDown(active_end){
    let end = this.formatIosDate(active_end);
    end +=' 23:59:59' ;
    let down = new Date(end).getTime() - Number(new Date().getTime().toFixed(0));
    this.setData({
      time:down
    })
  },
  formatIosDate(date){
    return date.replace(/\-/g, '/')
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  getDailyData(day) {
    this.data.acticeDay = day || (this.data.acticeMonth + '-' + new Date().getDate());
    this.setData({
      acticeDay: this.data.acticeDay
    })
    app._getNetWork({
      url: app.getNetAddresss("plugin.task-package.frontend.daily.info"),
      data: {
        record_id: this.data.id,
        day: this.data.acticeDay
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let {
            can_recovery,
            video,
            share,
            article,
            usable_name
          } = res.data;
          this.setData({
            usable_name: usable_name || '可用爱心值',
            can_recovery: can_recovery || false,
            videoArr: video || [],
            shareVideoArr: share || [],
            article: article || []
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  toRecord() {
    wx.navigateTo({
      url: '/packageI/taskPackage/record/record?type=1&id=' + this.data.info.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      showShare:false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})